/**
 * Created by haowanh5 on 2016/11/1.
 */

// This Class Is Design For Handle Collision Among Players and Different Kinds of Walls

"use strict";

const Logger = require("pomelo-logger").getLogger("CollisionHandler", __filename);
const Collision = require("./CollisionClass");

function CollisionHandler(gameServer) {
    this.gameServer = gameServer;
}

module.exports = CollisionHandler;

CollisionHandler.BOX_TYPE_CIRCLE    = Collision.BOX_TYPE_CIRCLE;
CollisionHandler.BOX_TYPE_RECTANGLE = Collision.BOX_TYPE_RECTANGLE;

CollisionHandler.COLLISION_PASS     = Collision.COLLISION_PASS;
CollisionHandler.COLLISION_STOP     = Collision.COLLISION_STOP;

/**
 * 碰撞检测
 *
 * @param {Player|Props} one Objects That Can Move
 * @param {GameObject|Player|Block|Drop|Props} other All Kinds Of Objects
 */
CollisionHandler.prototype.collide = function (one, other) {

    if (!other || !one || one.objectId == other.objectId) return false;

    var speed = one.getTankSpeedValue();
    // The Position Of One At Next Loop
    var nextPos = one.position.clone().add(speed);

    if (other.collision.boxType == Collision.BOX_TYPE_CIRCLE) {

        return other.getRange().intersectsCircle({"radian":one.collision.boxSize, "position": nextPos});

    } else if (other.collision.boxType == Collision.BOX_TYPE_RECTANGLE) {

        // Distance Between "one" And "other" At Next Loop, Todo: Ignore The Effect That Caused By Moment Of Object "other"
        var distance = nextPos.sub(other.position);

        // If distance Is Less Than The Sum Of Radians From Two Cycles, They Collide
        var collideDistance = one.collision.boxSize + other.collision.boxSize;
        return distance <= collideDistance;

    } else return false;
};